package com.myx.servlet.admin.news;

import com.myx.pojo.News;
import com.myx.pojo.User;
import com.myx.service.news.NewsService;
import com.myx.service.news.NewsServiceImpl;
import com.myx.tool.Constant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "ModifyNewsServlet",value = "/ModifyNewsServlet")
public class ModifyNewsServlet extends HttpServlet {
    NewsService newsService=new NewsServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Integer newsId = Integer.parseInt(request.getParameter("newsId"));

        String categoryIdStr = request.getParameter("categoryId");
        Integer categoryId=null;
        if (categoryIdStr != null) {
            categoryId = Integer.parseInt(categoryIdStr);

        }
        String newsTitle = request.getParameter("newsTitle");
        String newsSummary = request.getParameter("newsSummary");
        String newsContent = request.getParameter("newsContent");
        String imageUpload = request.getParameter("imageUpload");
        News news=new News();
        news.setId(newsId);
        news.setCategoryId(categoryId);
        news.setTitle(newsTitle);
        news.setSummary(newsSummary);
        news.setContent(newsContent);
        news.setPicPath(imageUpload);

        User loginUser = (User) request.getSession().getAttribute(Constant.LOGIN_SESSION);
        news.setModifyBy(loginUser.getId());
        news.setModifyDate(new Date());
        boolean isModify=newsService.modifyNews(news);
        if (isModify) {
            request.getRequestDispatcher("/jsp/admin/news/index.jsp").forward(request,response);
        }else {
            request.setAttribute(Constant.MODIFY_FAIL,"修改失败，请重新尝试");
            request.getRequestDispatcher("/jsp/admin/news/newModify.jsp?newsId="+newsId).forward(request,response);

        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);

    }
}
